﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ElectronicsMarket.Service.IService;
using ElectronicsMarket.Service.Models;

namespace ElectronicsMarket.Service.ServiceImpl
{
    public class StaffServiceImpl:IStaffService
    {
        ElectronicsMarketDBClassesDataContext eMEntity;
        public StaffServiceImpl()
        {
            eMEntity = new ElectronicsMarketDBClassesDataContext();
        }
        public List<STAFF> getAllStaff()
        {
            List<STAFF> lststaft = new List<STAFF>();
            var allstaff = from asf in eMEntity.STAFFs select asf;
            foreach (STAFF staff in allstaff)
                lststaft.Add(staff);

            return lststaft;
        }
        public void insertStaff(STAFF staff)
        {
            eMEntity.STAFFs.InsertOnSubmit(staff);
            try
            {
                eMEntity.SubmitChanges();
            }
            catch
            {
                throw new NotImplementedException();
            }
        }
        public void deleteStaff(STAFF staff)
        {
            STAFF staf = eMEntity.STAFFs.Single(s => s.STAFF_ID == staff.STAFF_ID);
            eMEntity.STAFFs.DeleteOnSubmit(staf);
            try
            {
                eMEntity.SubmitChanges();
            }
            catch
            {
                throw new NotImplementedException();
            }
        }

        public void updateStaff(STAFF staff)
        {
            STAFF st = eMEntity.STAFFs.Single(s => s.STAFF_ID == staff.STAFF_ID);
            
            st.STAFF_SEX = staff.STAFF_SEX;
            st.STAFF_PHONE = staff.STAFF_PHONE;
            st.STAFF_PASSWORD = staff.STAFF_PASSWORD;
            st.STAFF_MIDDLE_NAME = staff.STAFF_MIDDLE_NAME;
            st.STAFF_LEVEL = staff.STAFF_LEVEL;
            st.STAFF_LAST_NAME = staff.STAFF_LAST_NAME;
            st.STAFF_FISRT_NAME = staff.STAFF_FISRT_NAME;
            st.STAFF_DEPARMENT_ID = staff.STAFF_DEPARMENT_ID;
            st.STAFF_DAY_TO_WORK = staff.STAFF_DAY_TO_WORK;
            st.STAFF_DAY_ON_LEAVE = staff.STAFF_DAY_ON_LEAVE;
            st.STAFF_BIRTHDAY = staff.STAFF_BIRTHDAY;
            st.STAFF_ADDRESS = staff.STAFF_ADDRESS;

            eMEntity.SubmitChanges();
        }

        public STAFF getStaffByName(string name,string pass)
        {
            int numrecord = eMEntity.STAFFs.Count(st => st.STAFF_SYS_NAME == name);
            if(numrecord>0)
            {
            STAFF staff = eMEntity.STAFFs.Single(s => s.STAFF_SYS_NAME == name&& s.STAFF_PASSWORD==pass);
            return staff;
            }
            else
            {
                return null;
            }
        }
        public long getLastStaffId()
        {
            if (eMEntity.STAFFs.ToList() == null)
                return 0;
            else
            {
                return eMEntity.STAFFs.OrderByDescending(o => o.STAFF_ID).Select(o => o).First().STAFF_ID;
            }
        }
    }
}